package com.example.blogdemo.controller;

import com.example.blogdemo.entity.HostHolder;
import com.example.blogdemo.entity.User;
import com.example.blogdemo.service.LikeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author Anita
 * @Date 2017/12/21 17:56
 */
@Controller
public class LikeController {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private LikeService likeService;

    @RequestMapping("/like/{articleId}")
    public String like(@PathVariable("articleId")int articleId){
        User user = hostHolder.getUser();
        if(user==null)
            return "redirect:/article/"+articleId;

        likeService.like(user.getId(),articleId);
        return "redirect:/article/"+articleId;
    }

    @RequestMapping("/dislike/{articleId}")
    public String dislike(@PathVariable("articleId")int articleId){
        User user = hostHolder.getUser();
        if(user ==null)
            return "redirect:/article/"+articleId;

        likeService.dislike(user.getId(),articleId);
        return "redirect:/article/"+articleId;
    }



}
